package com.csun.cmny.provider.controller;


import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.DoctorWorkMedicalRecordDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcAttendance;
import com.csun.cmny.provider.model.domain.OmcCMFood;
import com.csun.cmny.provider.model.domain.OmcCMFoodInspection;
import com.csun.cmny.provider.model.domain.OmcNursingShiftChangeRecordRemark;
import com.csun.cmny.provider.model.dto.OldManSelectDto;
import com.csun.cmny.provider.model.dto.OmcCMFoodDto;
import com.csun.cmny.provider.model.dto.OmcCMFoodInspectionDto;
import com.csun.cmny.provider.model.dto.SendWebSocketRequest;
import com.csun.cmny.provider.model.dto.disinfection.RefrigeratorAndMicrowaveDisinfectionRecordSelectDto;
import com.csun.cmny.provider.model.dto.nursingShiftChangeRecord.NursingShiftChangeSelectDto;
import com.csun.cmny.provider.model.vo.disinfection.RefrigeratorAndMicrowaveDisinfectionRecordVo;
import com.csun.cmny.provider.model.vo.excrement.ExcrementRecordVo;
import com.csun.cmny.provider.model.vo.nursingShiftChangeRecord.NursingShiftChangeRecordParticularsVo;
import com.csun.cmny.provider.model.vo.nursingShiftChangeRecord.NursingShiftChangeRecordVo;
import com.csun.cmny.provider.model.vo.nursingShiftChangeRecord.NursingShiftChangeVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.*;
import com.csun.cmny.provider.vo.DoctorWorkMedicalRecordVo;
import com.csun.cmny.provider.vo.OldManSupVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-26 16:08
 * @Description:
 */
@RestController
@RequestMapping(value = "/api")
public class OmcApiController extends BaseController {

    @Autowired
    private OpcWebSocketService opcWebSocketService;
    @Resource
    private CmnyMessageSource  cmnyMessageSource;
    @Resource
    private OmcAttendanceService attendanceService;
    @Resource
    private OmcOldmanExcrementRecordService excrementRecordService;
    @Resource
    private OmcNursingShiftChangeService changeService;
    @Resource
    private OmcNursingShiftChangeRecordService shiftChangeRecordService;
    @Resource
    private OmcCMDisinfectionService disinfectionService;
    @Resource
    private OmcCMFoodService omcCMFoodService;
    @Resource
    private ChongmingDoctorWorkService chongmingDoctorWorkService;
    @Resource
    private OldmanService oldmanService;

    /**
     * 推送消息
     */
    @PostMapping(value = "/sendWebsocketMessage")
    public Wrapper sendMessageWithRoleCodeList(@RequestBody SendWebSocketRequest sendWebSocketRequest) {

        opcWebSocketService.sendWebSocketMessage(sendWebSocketRequest);

//        return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    @RequestMapping("/down")
    public void down(HttpServletResponse response) {

        String filename = cmnyMessageSource.getMessage("attendance.excel.name", null);
        String titleName = cmnyMessageSource.getMessage("attendance.excel.name", null);
        String sheetName = cmnyMessageSource.getMessage("attendance.excel.name", null);

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");
            //  JxlExcelUtils.fcReportToExcel(response, dmcDeviceAlertLifeRoomVos, sheetName);

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(sheetName, 0);
            Label staffName = new Label(0, 0, cmnyMessageSource.getMessage("attendance.excel.staffName", null));
            Label attendanceTime=new Label(1,0,cmnyMessageSource.getMessage("attendance.excel.attendanceTime", null));
            Label closingTime=new Label(2,0,cmnyMessageSource.getMessage("attendance.excel.closingTime", null));
            Label theDate=new Label(3,0,cmnyMessageSource.getMessage("attendance.excel.theDate", null));
            Label remark=new Label(4,0,cmnyMessageSource.getMessage("attendance.excel.remark", null));

            //创建画笔
            //第一种画笔
            WritableFont wft = new WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            WritableCellFormat wcfmt = new WritableCellFormat(wft);
            wcfmt.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            //设置水平居中
            wcfmt.setAlignment(jxl.format.Alignment.CENTRE);
            //设置垂直居中
            wcfmt.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            wcfmt.setWrap(true);

            //第二种画笔
            WritableFont wft1 = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            WritableCellFormat wcfmt1 = new WritableCellFormat(wft1);
            wcfmt1.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfmt1.setAlignment(jxl.format.Alignment.CENTRE);

            //第三种画笔
            WritableFont wft2 = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            WritableCellFormat wcfmt2 = new WritableCellFormat(wft2);
            wcfmt2.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            //设置水平居中
            wcfmt2.setAlignment(jxl.format.Alignment.CENTRE);
            //设置垂直居中
            wcfmt2.setVerticalAlignment(VerticalAlignment.CENTRE);
            wcfmt2.setWrap(true);

            //设置列宽
            sheet.setColumnView(0, 15);
            sheet.setColumnView(1, 30);
            sheet.setColumnView(2, 30);
            sheet.setColumnView(3, 30);
            sheet.setColumnView(4, 30);
            sheet.addCell(staffName);
            sheet.addCell(attendanceTime);
            sheet.addCell(closingTime);
            sheet.addCell(theDate);
            sheet.addCell(remark);

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(consumes = "multipart/form-data", value = "/save")
    @ApiOperation(httpMethod = "POST", value = "上传考勤")
    public Wrapper save (HttpServletRequest request) throws BiffException, IOException, WriteException, BiffException, ParseException {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        MultipartFile file = multipartRequest.getFile("file");

        Workbook wb = Workbook.getWorkbook(file.getInputStream());// 获得工作薄

        Sheet st = wb.getSheet(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<OmcAttendance> omcAttendances = new ArrayList<>();
        for (int i = 1; i <st.getRows(); i++) {
            OmcAttendance omcAttendance = new OmcAttendance();

            omcAttendance.setStaffName(st.getCell(0,i).getContents().trim());
            omcAttendance.setAttendanceTime(st.getCell(1,i).getContents().trim());
            omcAttendance.setClosingTime(st.getCell(2,i).getContents().trim());
            try {
                omcAttendance.setTheDate(simpleDateFormat.parse(st.getCell(3,i).getContents().trim()));
            }catch (Exception e) {
                throw new RuntimeException("日期格式应为yyyy-MM-dd");
            }

            omcAttendance.setRemark(st.getCell(4,i).getContents().trim());
            omcAttendance.setOperator(getLoginAuthDto().getUserName());
            omcAttendance.setUserId(getLoginAuthDto().getUserId());
            omcAttendance.setGroupId(getLoginAuthDto().getGroupId());

            omcAttendances.add(omcAttendance);
        }

        int save=attendanceService.saves(omcAttendances);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    /*导出老人大便记录*/
    @RequestMapping(value = "/downExcrementRecordExcel")
    public Wrapper selectExcrementRecord(HttpServletResponse response,Integer years,Integer theMonths) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String[] dates= DateUtils.forTimeByYearsAndMonths(years,theMonths);

        //查询老人大便记录
        List<ExcrementRecordVo> list = excrementRecordService.selectExcrementRecordByTime(dates[0], dates[1]);

        //整理大便数据
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                String[] recordDate = simpleDateFormat.format(list.get(i).getRecordDate()).split("-");
                String[] recordDate1 = simpleDateFormat.format(list.get(j).getRecordDate()).split("-");
                String date = recordDate[2];
                String date1 = recordDate1[2];
                Long oldManId = list.get(i).getOldManId();
                Long oldManId1 = list.get(j).getOldManId();
                if (date.equals(date1)&&oldManId==oldManId1) {
                    list.get(i).setNumbers(list.get(i).getNumbers()+list.get(j).getNumbers());
                    list.remove(j);
                }
            }
        }

        //写入表格名
        String[] month = {theMonths.toString()};
        String filename = cmnyMessageSource.getMessage("excel.excrement.excelName", month);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.numbers", null));
        names.add(cmnyMessageSource.getMessage("excel.bedNo", null));
        names.add(cmnyMessageSource.getMessage("excel.name", null));

        try {

            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            sheet.setRowView(0,600);

            //画上实线表格
            for (int i = 0; i <100; i++) {
                for (int j = 0; j < 34; j++) {
                    sheet.addCell(new Label(j,i+1,null,ExcelUtil.wft1()));
                }
            }

            //写入序号
            sheet.setColumnView(0,7);
            sheet.addCell(new Label(0,0,names.get(0),ExcelUtil.wft2()));

            //写入表头名
            for (Integer i = 1; i < names.size(); i++) {

                //设置列宽
                sheet.setColumnView(i, 10);
                //写入列名
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft2()));

            }

            //写入日期
            for (int i = 0; i < 31; i++) {

                String s = String.valueOf(i+1);
                //设置列宽
                sheet.setColumnView(names.size()+i, 7);
                //写入列名
                sheet.addCell(new Label(names.size()+i,0,s,ExcelUtil.wft2()));
            }

            //写入数据
            Map<Long,Integer> map = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {

                //写入大便信息
                String recordDate = simpleDateFormat.format(list.get(i).getRecordDate());
                String[] split = recordDate.split("-");
                int day = Integer.parseInt(split[2]);
                for (int j = 0; j < 31; j++) {
                    if (day==(j+1)) {
                        if (map.containsKey(list.get(i).getOldManId())){
                            sheet.addCell(new Label(names.size() + j, map.get(list.get(i).getOldManId())+1, list.get(i).getNumbers().toString(), ExcelUtil.wft1()));
                        }else {
                            String number = String.valueOf(i + 1);
                            String bedNo = list.get(i).getBedNo();
                            String oldManName = list.get(i).getOldManName();

                            //写入字段
                            sheet.addCell(new Label(0,map.size()+1,number,ExcelUtil.wft1()));
                            sheet.addCell(new Label(1,map.size()+1,bedNo,ExcelUtil.wft1()));
                            sheet.addCell(new Label(2,map.size()+1,oldManName,ExcelUtil.wft1()));

                            sheet.addCell(new Label(names.size() + j, map.size()+1, list.get(i).getNumbers().toString(), ExcelUtil.wft1()));
                            map.put(list.get(i).getOldManId(),map.size());
                        }
                    }
                }
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success",null));
    }

    //导出交接班记录
    @RequestMapping(value = "/downNursingShiftChangeRecord")
    public Wrapper downNursingShiftChangeRecord(HttpServletResponse response,Long id) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //交接班记录数据整理排序
        NursingShiftChangeSelectDto nursingShiftChangeSelectDto = new NursingShiftChangeSelectDto();
        nursingShiftChangeSelectDto.setId(id);
        List<NursingShiftChangeVo>list=changeService.selectShiftChangeRecord(nursingShiftChangeSelectDto);

        if (list.size()>1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("excel.data.error", null));
        }

        List<NursingShiftChangeRecordVo> recordVos = list.get(0).getRecordVos();

        if (recordVos.size()>2){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("excel.data.error", null));
        }else if (recordVos.size()<2){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("excel.data.wait.error", null));
        }
        recordVos.sort((x, y) -> Double.compare(x.getChangeStatus(), y.getChangeStatus()));

        List<NursingShiftChangeRecordParticularsVo> nursingShiftChangeRecordParticularsVo1=shiftChangeRecordService.selectNursingShiftChangeRecordById(recordVos.get(0).getId());
        List<NursingShiftChangeRecordParticularsVo> nursingShiftChangeRecordParticularsVo2=shiftChangeRecordService.selectNursingShiftChangeRecordById(recordVos.get(1).getId());

        if (nursingShiftChangeRecordParticularsVo1.size()>1||nursingShiftChangeRecordParticularsVo2.size()>1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("excel.data.error", null));
        }

        NursingShiftChangeRecordParticularsVo record1 = nursingShiftChangeRecordParticularsVo1.get(0);
        List<OmcNursingShiftChangeRecordRemark> remarks1 = record1.getRemarks();
        remarks1.sort((x, y) -> Double.compare(x.getOldManId(), y.getOldManId()));
        NursingShiftChangeRecordParticularsVo record2 = nursingShiftChangeRecordParticularsVo2.get(0);
        List<OmcNursingShiftChangeRecordRemark> remarks2 = record2.getRemarks();
        remarks2.sort((x, y) -> Double.compare(x.getOldManId(), y.getOldManId()));


        //写入表格名
        Date createdTime = list.get(0).getCreatedTime();
        String startTime = simpleDateFormat.format(createdTime);
        String[] date = startTime.split("-");
        String filename = cmnyMessageSource.getMessage("excel.shift.change.excelName", date);


        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            //写入班次抬头
            sheet.setColumnView(0, 20);
            sheet.addCell(new Label(0,0,cmnyMessageSource.getMessage("excel.nursing",null),ExcelUtil.wft2()));

            sheet.setColumnView(1,60);
            sheet.addCell(new Label(1,0,cmnyMessageSource.getMessage("excel.nursing.shift",null),ExcelUtil.wft2()));

            sheet.setColumnView(2,60);
            sheet.addCell(new Label(2,0,cmnyMessageSource.getMessage("excel.nursing.change",null),ExcelUtil.wft2()));

            //写入人数变化信息-----第一行
            sheet.setRowView(0,600);
            sheet.addCell(new Label(0,1,cmnyMessageSource.getMessage("excel.people.numbers",null),ExcelUtil.wft2()));
            //写入总人数和实有人数
            sheet.setRowView(1,600);
            String[] allplepeo1={record1.getAllPeople().toString(),record1.getRealityPeople().toString()};
            String[] allplepeo2={record2.getAllPeople().toString(),record2.getRealityPeople().toString()};
            sheet.addCell(new Label(1,1,cmnyMessageSource.getMessage("excel.people.numbers.line.one",allplepeo1),ExcelUtil.wft1()));
            sheet.addCell(new Label(2,1,cmnyMessageSource.getMessage("excel.people.numbers.line.one",allplepeo2),ExcelUtil.wft1()));
            //写入新入院等人数
            sheet.setRowView(2,600);
            String[] newPeople1 = {record1.getNewPeople().toString(), record1.getShiftToPeople().toString(), record1.getReturnPeople().toString()};
            String[] newPeople2 = {record2.getNewPeople().toString(), record2.getShiftToPeople().toString(), record2.getReturnPeople().toString()};
            sheet.addCell(new Label(1,2,cmnyMessageSource.getMessage("excel.people.numbers.line.two",newPeople1),ExcelUtil.wft1()));
            sheet.addCell(new Label(2,2,cmnyMessageSource.getMessage("excel.people.numbers.line.two",newPeople2),ExcelUtil.wft1()));
            //写入出院等人数
            sheet.setRowView(3,600);
            String[] leavePeople1={record1.getLeaveHospitalPeople().toString(),record1.getRollOutPeople().toString(),record1.getLeavePeople().toString(),record1.getDiePeople().toString()};
            String[] leavePeople2={record2.getLeaveHospitalPeople().toString(),record2.getRollOutPeople().toString(),record2.getLeavePeople().toString(),record2.getDiePeople().toString()};
            sheet.addCell(new Label(1,3,cmnyMessageSource.getMessage("excel.people.numbers.line.three",leavePeople1),ExcelUtil.wft1()));
            sheet.addCell(new Label(2,3,cmnyMessageSource.getMessage("excel.people.numbers.line.three",leavePeople2),ExcelUtil.wft1()));
            //写入外出就诊人数
            sheet.setRowView(4,600);
            String[] goPeople1={record1.getGoOutPeople().toString()};
            String[] goPeople2={record2.getGoOutPeople().toString()};
            sheet.addCell(new Label(1,4,cmnyMessageSource.getMessage("excel.people.numbers.line.four",goPeople1),ExcelUtil.wft1()));
            sheet.addCell(new Label(2,4,cmnyMessageSource.getMessage("excel.people.numbers.line.four",goPeople2),ExcelUtil.wft1()));

            //写入交接班信息
            //sheet.addCell(new Label(0,4,cmnyMessageSource.getMessage("excel.bed.no.old.name",goPeople2),wcfmt2));
            for (int i = 0; i < remarks1.size(); i++) {

                sheet.setRowView(i+5,3000);
                //sheet.addCell(new Label(0,i+5,remarks1.get(i).getBedNo()+remarks1.get(i).getOldManName(),wcfmt2));
                sheet.addCell(new Label(1,i+5,remarks1.get(i).getOldManName()+":"+remarks1.get(i).getConditions(),ExcelUtil.wft1()));

            }
            for (int i = 0; i < remarks2.size(); i++) {

                sheet.setRowView(i+5,3000);
                sheet.addCell(new Label(2,i+5,remarks2.get(i).getOldManName()+":"+remarks2.get(i).getConditions(),ExcelUtil.wft1()));
            }

            //护工签名
            String[] nursingName1 = {record1.getNursingName()};
            String[] nursingName2 = {record2.getNursingName()};
            if (PublicUtil.isEmpty(remarks1)&&PublicUtil.isEmpty(remarks2)){
                sheet.setRowView(5,600);
                sheet.addCell(new Label(1,5,cmnyMessageSource.getMessage("excel.nursing.name.shift",nursingName1),ExcelUtil.wft2()));
                sheet.addCell(new Label(2,5,cmnyMessageSource.getMessage("excel.nursing.name.change",nursingName2),ExcelUtil.wft2()));
            }else if (remarks1.size()>remarks2.size()||remarks1.size()==remarks2.size()){
                sheet.setRowView(remarks1.size()+5,600);
                sheet.setRowView(remarks1.size()+5,600);
                sheet.addCell(new Label(1,remarks1.size()+5,cmnyMessageSource.getMessage("excel.nursing.name.shift",nursingName1),ExcelUtil.wft2()));
                sheet.addCell(new Label(2,remarks1.size()+5,cmnyMessageSource.getMessage("excel.nursing.name.change",nursingName2),ExcelUtil.wft2()));
            }else {
                sheet.setRowView(remarks2.size(),600);
                sheet.setRowView(remarks2.size()+5,600);
                sheet.addCell(new Label(1,remarks2.size()+5,cmnyMessageSource.getMessage("excel.nursing.name.shift",nursingName1),ExcelUtil.wft2()));
                sheet.addCell(new Label(2,remarks2.size()+5,cmnyMessageSource.getMessage("excel.nursing.name.change",nursingName2),ExcelUtil.wft2()));
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    /*导出冰箱微波炉消毒记录*/
    @RequestMapping(value = "/downRefrigeratorAndMicrowaveDisinfectionRecordExcel")
    public Wrapper downRefrigeratorAndMicrowaveDisinfectionRecordExcel(HttpServletResponse response,String startTime,String endTime) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //查询冰箱微波炉消毒记录
        RefrigeratorAndMicrowaveDisinfectionRecordSelectDto selectDto = new RefrigeratorAndMicrowaveDisinfectionRecordSelectDto();
        selectDto.setStartTime(startTime);
        selectDto.setEndTime(endTime);
        List<RefrigeratorAndMicrowaveDisinfectionRecordVo>list=disinfectionService.selectRefrigeratorAndMicrowaveDisinfectionRecord(selectDto);

        //写入表格名
        String filename = cmnyMessageSource.getMessage("excel.refrigeratorAndMicrowaveDisinfectionRecord",null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.date", null));
        names.add(cmnyMessageSource.getMessage("excel.refrigerator", null));
        names.add(cmnyMessageSource.getMessage("excel.microwave", null));
        names.add(cmnyMessageSource.getMessage("excel.disinfection.name", null));

        try {

            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            sheet.setRowView(0,600);
            for (int i = 0; i < names.size(); i++) {
                //写入表头
                sheet.setColumnView(i,30);
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft()));
            }

            for (int i = 0; i < list.size(); i++) {

                //画上实线表格
                for (int j = 0; j < 3; j++) {
                    sheet.addCell(new Label(j,i+1,null,ExcelUtil.wft1()));
                }

                String[] date = simpleDateFormat.format(list.get(i).getCreatedTime()).split(" ");
                sheet.addCell(new Label(0,i+1,date[0],ExcelUtil.wft1()));
                if (list.get(i).getTypeId()==3){
                    sheet.addCell(new Label(1,i+1,cmnyMessageSource.getMessage("excel.tick", null),ExcelUtil.wft1()));
                }else {
                    sheet.addCell(new Label(2,i+1,cmnyMessageSource.getMessage("excel.tick", null),ExcelUtil.wft1()));
                }
                sheet.addCell(new Label(3,i+1,list.get(i).getNursingName(),ExcelUtil.wft1()));

            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success",null));
    }

    //自带食品登记表下载
    @RequestMapping("/downCMFoodExcel")
    public void downCMFoodExcel(HttpServletResponse response,String startTime,String endTime) {
        //自带食品列表
        OmcCMFoodDto omcCMFoodDto = new OmcCMFoodDto();
        omcCMFoodDto.setStartQueryTime(startTime);
        omcCMFoodDto.setEndQueryTime(endTime);
        List<OmcCMFood> list = omcCMFoodService.queryFood(omcCMFoodDto);

        //表格名
        String filename = cmnyMessageSource.getMessage("excel.bring.food.registration", null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.date", null));
        names.add(cmnyMessageSource.getMessage("excel.oldName", null));
        names.add(cmnyMessageSource.getMessage("excel.bedNo", null));
        names.add(cmnyMessageSource.getMessage("excel.foodNameAndNumbers", null));
        names.add(cmnyMessageSource.getMessage("excel.expire.Date", null));
        names.add(cmnyMessageSource.getMessage("excel.family.members.signature", null));
        names.add(cmnyMessageSource.getMessage("excel.Signature.receiver", null));
        names.add(cmnyMessageSource.getMessage("excel.remark", null));

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //写入表头
            sheet.setRowView(0,600);
            for (int i = 0; i < names.size(); i++) {
                sheet.setColumnView(i,20);
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft2()));
            }

            //写入数据
            for (int i = 0; i < list.size(); i++) {
                sheet.setRowView(i+1,500);
                String[] dates = simpleDateFormat.format(list.get(i).getCreatedTime()).split(" ");
                String[] expireDates = simpleDateFormat.format(list.get(i).getExpireDate()).split(" ");
                sheet.addCell(new Label(0,i+1,dates[0],ExcelUtil.wft1()));
                sheet.addCell(new Label(1,i+1,list.get(i).getOldmanName(),ExcelUtil.wft1()));
                sheet.addCell(new Label(2,i+1,list.get(i).getBedNo(),ExcelUtil.wft1()));
                sheet.addCell(new Label(3,i+1,list.get(i).getFoodDetail(),ExcelUtil.wft1()));
                sheet.addCell(new Label(4,i+1,expireDates[0],ExcelUtil.wft1()));
                sheet.addCell(new Label(5,i+1,list.get(i).getFamilySignature(),ExcelUtil.wft1()));
                sheet.addCell(new Label(6,i+1,list.get(i).getCreator(),ExcelUtil.wft1()));
                sheet.addCell(new Label(7,i+1,list.get(i).getRemark(),ExcelUtil.wft1()));
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //日常检查及食品药品处理表格下载
    @RequestMapping("/downRoutineInspectionFoodExcel")
    public void downRoutineInspectionFoodExcel(HttpServletResponse response,String startTime,String endTime) {
        //日常检查及食品药品处理列表
        OmcCMFoodInspectionDto omcCMFoodInspectionDto = new OmcCMFoodInspectionDto();
        omcCMFoodInspectionDto.setStartQueryTime(startTime);
        omcCMFoodInspectionDto.setEndQueryTime(endTime);
        List<OmcCMFoodInspection> list = omcCMFoodService.queryInspection(omcCMFoodInspectionDto);

        //表格名
        String filename = cmnyMessageSource.getMessage("excel.routine.inspection.food", null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.have", null));
        names.add(cmnyMessageSource.getMessage("excel.not.have", null));
        names.add(cmnyMessageSource.getMessage("excel.not.have", null));
        names.add(cmnyMessageSource.getMessage("excel.past.due.food.name", null));
        names.add(cmnyMessageSource.getMessage("excel.discard", null));
        names.add(cmnyMessageSource.getMessage("excel.bring.back", null));

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //写入表头
            sheet.setRowView(0,600);

            //日期
            sheet.setColumnView(0,20);
            sheet.mergeCells(0,0,0,1);
            sheet.addCell(new Label(0,0,cmnyMessageSource.getMessage("excel.date", null),ExcelUtil.wft2()));

            //姓名
            sheet.setColumnView(1,20);
            sheet.mergeCells(1,0,0,1);
            sheet.addCell(new Label(1,0,cmnyMessageSource.getMessage("excel.name", null),ExcelUtil.wft2()));

            //食物放入冰箱标识
            sheet.setColumnView(2,10);
            sheet.setColumnView(3,10);
            sheet.mergeCells(2,0,3,0);
            sheet.addCell(new Label(2,0,cmnyMessageSource.getMessage("excel.food.in.refrigerator", null),ExcelUtil.wft2()));

            //过期食品或药品
            sheet.setColumnView(4,10);
            sheet.setColumnView(5,30);
            sheet.mergeCells(4,0,5,0);
            sheet.addCell(new Label(4,0,cmnyMessageSource.getMessage("excel.past.due.food", null),ExcelUtil.wft2()));

            //家属或老人处理意见
            sheet.setColumnView(6,15);
            sheet.setColumnView(7,15);
            sheet.mergeCells(6,0,7,0);
            sheet.addCell(new Label(6,0,cmnyMessageSource.getMessage("excel.family.old.opinion", null),ExcelUtil.wft2()));

            //其他表头
            for (int i = 0; i < names.size(); i++) {
                sheet.addCell(new Label(i+2,1,names.get(i),ExcelUtil.wft2()));
            }

            //画上实线
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < 8; j++) {
                    sheet.addCell(new Label(j,i+2,null,ExcelUtil.wft2()));
                }
            }

            String tick = cmnyMessageSource.getMessage("excel.tick", null);

            //写入数据
            for (int i = 0; i < list.size(); i++) {
                String[] dates = simpleDateFormat.format(list.get(i).getCreatedTime()).split(" ");
                //日期
                sheet.addCell(new Label(0,i+2,dates[0],ExcelUtil.wft1()));
                //姓名
                sheet.addCell(new Label(1,i+2,list.get(i).getOldmanName(),ExcelUtil.wft1()));
                //食物是否有放入冰箱标识
                if (list.get(i).getIsRefrigerated()){
                    sheet.addCell(new Label(2,i+2,tick,ExcelUtil.wft1()));
                }else {
                    sheet.addCell(new Label(3,i+2,tick,ExcelUtil.wft1()));
                }
                //是否有过期食品或药品
                if (PublicUtil.isEmpty(list.get(i).getExpiredFood())){
                    sheet.addCell(new Label(4,i+2,tick,ExcelUtil.wft1()));
                }else {
                    sheet.addCell(new Label(5,i+2,list.get(i).getExpiredFood(),ExcelUtil.wft1()));
                }
                //家属或老人处理意见
                if (list.get(i).getFamilyDisposition()==1){
                    sheet.addCell(new Label(6,i+2,tick,ExcelUtil.wft1()));
                }else if (list.get(i).getFamilyDisposition()==2){
                    sheet.addCell(new Label(7,i+2,tick,ExcelUtil.wft1()));
                }
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //病程记录表格下载
    @RequestMapping("/downMedicalRecordExcel")
    public void downMedicalRecordExcel(HttpServletResponse response,Long oldManId) {

        //病程记录列表
        DoctorWorkMedicalRecordDto doctorWorkMedicalRecordDto = new DoctorWorkMedicalRecordDto();
        doctorWorkMedicalRecordDto.setOldmanId(oldManId.toString());
        List<DoctorWorkMedicalRecordVo> list = chongmingDoctorWorkService.selectMedicalRecordToList(doctorWorkMedicalRecordDto);

        if (PublicUtil.isEmpty(list)){
            throw new OmcBizException(ErrorCodeEnum.OMC10091004);
        }

        //表格名
        String[] tableName={list.get(0).getOldmanName()};
        String filename = cmnyMessageSource.getMessage("excel.medical.record", tableName);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.oldName", null));
        names.add(cmnyMessageSource.getMessage("excel.medicalRecordNo", null));
        names.add(cmnyMessageSource.getMessage("excel.medicalRecord.string", null));
        names.add(cmnyMessageSource.getMessage("excel.diagnosis", null));
        names.add(cmnyMessageSource.getMessage("excel.plan", null));
        names.add(cmnyMessageSource.getMessage("excel.doctorName", null));
        names.add(cmnyMessageSource.getMessage("excel.date", null));

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //设置宽高度
            sheet.setRowView(0,600);
            sheet.setColumnView(0,15);
            sheet.setColumnView(1,25);
            sheet.setColumnView(2,40);
            sheet.setColumnView(3,40);
            sheet.setColumnView(4,40);
            sheet.setColumnView(5,15);
            sheet.setColumnView(6,20);

            //写入表头
            for (int i = 0; i < names.size(); i++) {
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft2()));
            }

            //写入数据
            for (int i = 0; i < list.size(); i++) {
                sheet.setRowView(i+1,3600);
                sheet.addCell(new Label(0,i+1,list.get(i).getOldmanName(),ExcelUtil.wft2()));
                sheet.addCell(new Label(1,i+1,list.get(i).getMedicalRecordNo(),ExcelUtil.wft1()));
                sheet.addCell(new Label(2,i+1,list.get(i).getMedicalRecord(),ExcelUtil.wft1()));
                sheet.addCell(new Label(3,i+1,list.get(i).getDiagnosis(),ExcelUtil.wft1()));
                sheet.addCell(new Label(4,i+1,list.get(i).getPlan(),ExcelUtil.wft1()));
                sheet.addCell(new Label(5,i+1,list.get(i).getDoctorName(),ExcelUtil.wft1()));
                String[] dates = simpleDateFormat.format(list.get(i).getMedicalTime()).split(" ");
                sheet.addCell(new Label(6,i+1,dates[0],ExcelUtil.wft1()));
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //机构版导入老人信息模板下载
    @RequestMapping("/downOldManTemplateByGroup")
    public void downOldManTemplateByGroup(HttpServletResponse response) {

        //表格名
        String filename = cmnyMessageSource.getMessage("excel.down.oldMan.template.group",null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.oldName", null));     //0
        names.add(cmnyMessageSource.getMessage("excel.down.IdCard", null)); //1
        names.add(cmnyMessageSource.getMessage("excel.down.fender", null)); //2
        names.add(cmnyMessageSource.getMessage("excel.down.SsCard", null)); //3
        names.add(cmnyMessageSource.getMessage("excel.down.birthday", null)); //4
        names.add(cmnyMessageSource.getMessage("excel.down.isDisabled", null)); //5
        names.add(cmnyMessageSource.getMessage("excel.down.disabledCard", null)); //6
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.mStatus", null)); //7
        names.add(cmnyMessageSource.getMessage("excel.down.personType", null)); //8
        names.add(cmnyMessageSource.getMessage("excel.down.family.address.oldman", null)); //9
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.economicSource",null)); //10
        names.add(cmnyMessageSource.getMessage("excel.down.ocommunity",null)); //11
        names.add(cmnyMessageSource.getMessage("excel.down.ccommunity",null)); //12
        names.add(cmnyMessageSource.getMessage("excel.down.relativePhone",null)); //13
        names.add(cmnyMessageSource.getMessage("excel.down.ccomphone",null)); //14
        names.add(cmnyMessageSource.getMessage("excel.down.isKeepAshes",null)); //15
        names.add(cmnyMessageSource.getMessage("excel.down.isDonate",null)); //16
        names.add(cmnyMessageSource.getMessage("excel.down.shenqing",null)); //17
        names.add(cmnyMessageSource.getMessage("excel.down.shenqing.sex",null)); //18
        names.add(cmnyMessageSource.getMessage("excel.down.shenqing.IDCard",null)); //19
        names.add(cmnyMessageSource.getMessage("excel.down.xiuyang",null)); //20
        names.add(cmnyMessageSource.getMessage("excel.down.shenqing.address",null)); //21
        names.add(cmnyMessageSource.getMessage("excel.down.shenqing.phone",null)); //22
        //names.add(cmnyMessageSource.getMessage("excel.down.specialCase",null));
        names.add(cmnyMessageSource.getMessage("excel.down.isOut",null)); //23
        names.add(cmnyMessageSource.getMessage("excel.down.xianzhi",null)); //24
        names.add(cmnyMessageSource.getMessage("excel.down.yizhi",null)); //25
        names.add(cmnyMessageSource.getMessage("excel.down.other",null)); //26

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            //设置宽高度
            sheet.setRowView(0,600);
            sheet.setColumnView(0,15);
            sheet.setColumnView(1,40);
            sheet.setColumnView(2,10);
            sheet.setColumnView(3,40);
            sheet.setColumnView(4,20);
            sheet.setColumnView(5,10);
            sheet.setColumnView(6,40);
            sheet.setColumnView(7,15);
            sheet.setColumnView(8,20);
            sheet.setColumnView(9,40);
            sheet.setColumnView(10,30);
            sheet.setColumnView(11,40);
            sheet.setColumnView(12,30);
            sheet.setColumnView(13,25);
            sheet.setColumnView(14,40);
            sheet.setColumnView(15,10);
            sheet.setColumnView(16,10);
            sheet.setColumnView(17,30);
            sheet.setColumnView(18,30);
            sheet.setColumnView(19,30);
            sheet.setColumnView(20,30);
            sheet.setColumnView(21,30);
            sheet.setColumnView(22,30);
            sheet.setColumnView(23,10);
            sheet.setColumnView(24,10);
            sheet.setColumnView(25,10);
            sheet.setColumnView(26,10);
            //写入表头
            for (int i = 0; i < names.size(); i++) {
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft2()));
            }
            //写入下拉选项框
            for (int i = 0; i < 300; i++) {

                sheet.setRowView(i,600);

                //性别
                Label gender = new Label(2, i+1,"  ");
                WritableCellFeatures wcf = new WritableCellFeatures();
                wcf.setDataValidationList(ExcelTemplateUtils.toGender());
                gender.setCellFeatures(wcf);
                sheet.addCell(gender);

                //是否残疾
                Label disable = new Label(5, i+1,"  ");
                WritableCellFeatures wcf1 = new WritableCellFeatures();
                wcf1.setDataValidationList(ExcelTemplateUtils.toDisable());
                disable.setCellFeatures(wcf1);
                sheet.addCell(disable);

                //老人婚姻状况
                Label mStatuss = new Label(7, i+1,"  ");
                WritableCellFeatures wcf3 = new WritableCellFeatures();
                wcf3.setDataValidationList(ExcelTemplateUtils.toMStatuss());
                mStatuss.setCellFeatures(wcf3);
                sheet.addCell(mStatuss);

                //老人人员类别
                Label peopleType = new Label(8, i+1,"  ");
                WritableCellFeatures wcf4 = new WritableCellFeatures();
                wcf4.setDataValidationList(ExcelTemplateUtils.toPeopleType());
                peopleType.setCellFeatures(wcf4);
                sheet.addCell(peopleType);

                //老人经济来源
                Label economicSources = new Label(10, i+1,"  ");
                WritableCellFeatures wcf5 = new WritableCellFeatures();
                wcf5.setDataValidationList(ExcelTemplateUtils.toEconomicSources());
                economicSources.setCellFeatures(wcf5);
                sheet.addCell(economicSources);

                //老人是否残疾
                /*Label isDisable = new Label(5, i+1,"  ");
                WritableCellFeatures wcf6 = new WritableCellFeatures();
                wcf6.setDataValidationList(ExcelTemplateUtils.toDisable());
                isDisable.setCellFeatures(wcf6);
                sheet.addCell(isDisable);*/

                //老人是否保留骨灰
                Label keep = new Label(15, i+1,"  ");
                WritableCellFeatures wcf7 = new WritableCellFeatures();
                wcf7.setDataValidationList(ExcelTemplateUtils.toIsKeep());
                keep.setCellFeatures(wcf7);
                sheet.addCell(keep);

                //老人是否捐赠遗体
                Label isDonate = new Label(16, i+1,"  ");
                WritableCellFeatures wcf8 = new WritableCellFeatures();
                wcf8.setDataValidationList(ExcelTemplateUtils.toIsDonate());
                isDonate.setCellFeatures(wcf8);
                sheet.addCell(isDonate);

                //申请人性别
                Label isApply = new Label(18, i+1,"  ");
                WritableCellFeatures wcf13 = new WritableCellFeatures();
                wcf13.setDataValidationList(ExcelTemplateUtils.toApplySex());
                isApply.setCellFeatures(wcf13);
                sheet.addCell(isApply);

                //申请人与老人关系
                Label isApplyRel = new Label(20, i+1,"  ");
                WritableCellFeatures wcf14 = new WritableCellFeatures();
                wcf14.setDataValidationList(ExcelTemplateUtils.toApplyRelation());
                isApplyRel.setCellFeatures(wcf14);
                sheet.addCell(isApplyRel);

                //同意休养人员独立外出活动
                Label isOut = new Label(23, i+1,"  ");
                WritableCellFeatures wcf9 = new WritableCellFeatures();
                wcf9.setDataValidationList(ExcelTemplateUtils.toIsOut());
                isOut.setCellFeatures(wcf9);
                sheet.addCell(isOut);

                //是否将休养人员限制在床上或者房内
                Label isLimit = new Label(24, i+1,"  ");
                WritableCellFeatures wcf10 = new WritableCellFeatures();
                wcf10.setDataValidationList(ExcelTemplateUtils.toIsLimit());
                isLimit.setCellFeatures(wcf10);
                sheet.addCell(isLimit);

                //休养人紧急情况下是否允许院方诊治
                Label isTreat = new Label(25, i+1,"  ");
                WritableCellFeatures wcf11 = new WritableCellFeatures();
                wcf11.setDataValidationList(ExcelTemplateUtils.toIsTreat());
                isTreat.setCellFeatures(wcf11);
                sheet.addCell(isTreat);

                //夜间是否吵闹干扰他人
                Label isNoisy = new Label(26, i+1,"  ");
                WritableCellFeatures wcf12 = new WritableCellFeatures();
                wcf12.setDataValidationList(ExcelTemplateUtils.toIsNoisy());
                isNoisy.setCellFeatures(wcf12);
                sheet.addCell(isNoisy);
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //社区版导出老人信息
    @RequestMapping("/downOldManInformation")
    public void downOldManByGroup(HttpServletResponse response, OldManSelectDto oldManSelectDto) {
        oldManSelectDto.setGroupId(getLoginAuthDto().getGroupId());
        List<OldManSupVo> list = oldmanService.selectOldManByExcel(oldManSelectDto);

        List<Integer> fields = oldManSelectDto.getFields();

        //表格名
        String filename = cmnyMessageSource.getMessage("excel.down.oldMan.template",null);

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            //设置宽高度
            sheet.setRowView(0,600);

            //写入表头
            if (DataUtils.listIfHaveInteger(fields,0)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 0);
                sheet.setColumnView(pointer,15);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.name", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,1)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 1);
                sheet.setColumnView(pointer,10);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.age", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,2)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 2);
                sheet.setColumnView(pointer,7);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.fender.enter", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,3)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 3);
                sheet.setColumnView(pointer,25);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.IdCard", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,4)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 4);
                sheet.setColumnView(pointer,18);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.phone", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,5)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 5);
                sheet.setColumnView(pointer,30);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.family.address.oldman", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,6)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 6);
                sheet.setColumnView(pointer,15);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.bedNo", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,7)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 7);
                sheet.setColumnView(pointer,16);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.admission.time", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,8)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 8);
                sheet.setColumnView(pointer,15);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.operator", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,9)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 9);
                sheet.setColumnView(pointer,7);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.is.pay", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,10)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 10);
                sheet.setColumnView(pointer,8);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.state.evaluation", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,11)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 11);
                sheet.setColumnView(pointer,10);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.levelCareName", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,12)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 12);
                sheet.setColumnView(pointer,7);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.the.type", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,13)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 13);
                sheet.setColumnView(pointer,15);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.emergency.contact", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,14)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 14);
                sheet.setColumnView(pointer,18);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.emergency.telephone.number", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,15)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 15);
                sheet.setColumnView(pointer,12);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.relation.for.oldman.enter", null),ExcelUtil.wft2()));
            }
            if (DataUtils.listIfHaveInteger(fields,16)){
                Integer pointer = DataUtils.getPointerToInteger(fields, 16);
                sheet.setColumnView(pointer,30);
                sheet.addCell(new Label(pointer,0,cmnyMessageSource.getMessage("excel.down.family.address.ega", null),ExcelUtil.wft2()));
            }

            //写入数据
            for (int i = 0; i < list.size(); i++) {
                sheet.setRowView(i+1,600);
                if(DataUtils.listIfHaveInteger(fields,0)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 0),i+1,list.get(i).getOldName(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(fields,1)){
                    if (PublicUtil.isNotEmpty(list.get(i).getOldAge())){
                        sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 1),i+1,list.get(i).getOldAge().toString(),ExcelUtil.wft1()));
                    }
                }
                if(DataUtils.listIfHaveInteger(fields,2)){
                    if (PublicUtil.isNotEmpty(list.get(i).getSex())&&list.get(i).getSex()==1){
                        sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 2),i+1,cmnyMessageSource.getMessage("excel.down.sex.woman",null),ExcelUtil.wft1()));
                    }else {
                        sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 2),i+1,cmnyMessageSource.getMessage("excel.down.sex.man",null),ExcelUtil.wft1()));
                    }
                }
                if(DataUtils.listIfHaveInteger(fields,3)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 3),i+1,list.get(i).getOldIdCard(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),4)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 4),i+1,list.get(i).getOldPhone(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),5)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 5),i+1,list.get(i).getOldAddress(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),6)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 6),i+1,list.get(i).getNewBedNo(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),7)){
                    sheet.addCell(new Label( DataUtils.getPointerToInteger(fields, 7),i+1,list.get(i).getInDate(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),8)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 8),i+1,list.get(i).getCreator(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),9)){
                    if (PublicUtil.isNotEmpty(list.get(i).getIsPayed())){
                        if (list.get(i).getIsPayed()==1){
                            sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 9),i+1,cmnyMessageSource.getMessage("excel.down.pay.yes",null),ExcelUtil.wft1()));
                        }else {
                            sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 9),i+1,cmnyMessageSource.getMessage("excel.down.pay.no",null),ExcelUtil.wft1()));
                        }
                    }
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),10)){
                    if (PublicUtil.isNotEmpty(list.get(i).getStatus())){
                        sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 10),i+1,OldManStatusUtils.toStatus(list.get(i).getStatus()),ExcelUtil.wft1()));
                    }
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),11)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 11),i+1,list.get(i).getLevelCareName(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),12)){
                    if (PublicUtil.isNotEmpty(list.get(i).getServiceType())){
                        sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 12),i+1,OldManStatusUtils.toServiceType(list.get(i).getServiceType()),ExcelUtil.wft1()));
                    }
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),13)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 13),i+1,list.get(i).getEgNamea(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),14)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 14),i+1,list.get(i).getEgPhonea(),ExcelUtil.wft1()));
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),15)){
                    if (PublicUtil.isNotEmpty(list.get(i).getRelationshipa())){
                        sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 15),i+1, OldManStatusUtils.toRelation(list.get(i).getRelationshipa()),ExcelUtil.wft1()));
                    }
                }
                if(DataUtils.listIfHaveInteger(oldManSelectDto.getFields(),16)){
                    sheet.addCell(new Label(DataUtils.getPointerToInteger(fields, 16),i+1,list.get(i).getEgaAddress(),ExcelUtil.wft1()));
                }
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //社区版导出老人信息
   /* @RequestMapping("/downOldManByShequ")
    public void downOldManByShequ(HttpServletResponse response, OldManSelectDto oldManSelectDto) {

        List<OldManSupVo> list = oldmanService.selectOldManByExcel(oldManSelectDto);

        //表格名
        String filename = cmnyMessageSource.getMessage("excel.down.oldMan.shequ",null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.name", null));
        names.add(cmnyMessageSource.getMessage("excel.down.age", null));
        names.add(cmnyMessageSource.getMessage("excel.down.fender", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.IdCard", null));
        names.add(cmnyMessageSource.getMessage("excel.down.family.address.oldman", null));
        names.add(cmnyMessageSource.getMessage("excel.down.emergency.contact", null));
        names.add(cmnyMessageSource.getMessage("excel.down.relation.for.oldman", null));
        names.add(cmnyMessageSource.getMessage("excel.down.phone", null));
        names.add(cmnyMessageSource.getMessage("excel.down.family.address.ega", null));
        names.add(cmnyMessageSource.getMessage("excel.down.state.evaluation", null));
        names.add(cmnyMessageSource.getMessage("excel.levelCareName", null));
        names.add(cmnyMessageSource.getMessage("excel.the.type", null));

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            //设置宽高度
            sheet.setRowView(0,600);
            sheet.setColumnView(0,25);
            sheet.setColumnView(1,10);
            sheet.setColumnView(2,10);
            sheet.setColumnView(3,35);
            sheet.setColumnView(4,30);
            sheet.setColumnView(5,20);
            sheet.setColumnView(6,20);
            sheet.setColumnView(7,20);
            sheet.setColumnView(8,30);
            sheet.setColumnView(9,15);

            //写入表头
            for (int i = 0; i < names.size(); i++) {
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft2()));
            }

            //写入数据
            for (int i = 0; i < list.size(); i++) {
                sheet.setRowView(i+1,600);
                sheet.addCell(new Label(0,i+1,list.get(i).getOldName(),ExcelUtil.wft1()));
                sheet.addCell(new Label(1,i+1,list.get(i).getOldAge().toString(),ExcelUtil.wft1()));

                if (list.get(i).getSex()==0){
                    sheet.addCell(new Label(2,i+1,cmnyMessageSource.getMessage("excel.down.sex.man",null),ExcelUtil.wft1()));
                }else {
                    sheet.addCell(new Label(2,i+1,cmnyMessageSource.getMessage("excel.down.sex.woman",null),ExcelUtil.wft1()));
                }

                sheet.addCell(new Label(3,i+1,list.get(i).getOldIdCard(),ExcelUtil.wft1()));
                sheet.addCell(new Label(4,i+1,list.get(i).getOldAddress(),ExcelUtil.wft1()));
                sheet.addCell(new Label(5,i+1,list.get(i).getEgNamea(),ExcelUtil.wft1()));
                if (PublicUtil.isNotEmpty(list.get(i).getRelationshipa())){
                    sheet.addCell(new Label(6,i+1, OldManStatusUtils.toRelation(list.get(i).getRelationshipa()),ExcelUtil.wft1()));
                }
                sheet.addCell(new Label(7,i+1,list.get(i).getOldPhone(),ExcelUtil.wft1()));
                sheet.addCell(new Label(8,i+1,list.get(i).getEgaAddress(),ExcelUtil.wft1()));
                if (PublicUtil.isNotEmpty(list.get(i).getStatus())){
                    sheet.addCell(new Label(9,i+1,OldManStatusUtils.toStatus(list.get(i).getStatus()),ExcelUtil.wft1()));
                }
                if (PublicUtil.isNotEmpty(list.get(i).getLevelCareName())){
                    sheet.addCell(new Label(10,i+1,list.get(i).getLevelCareName(),ExcelUtil.wft1()));
                }
                if (PublicUtil.isNotEmpty(list.get(i).getServiceType())){
                    sheet.addCell(new Label(11,i+1,OldManStatusUtils.toServiceType(list.get(i).getServiceType()),ExcelUtil.wft1()));
                }
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    //社区版导入老人信息模板下载
    @RequestMapping("/downOldManTemplateByShequ")
    public void downOldManTemplateByShequ(HttpServletResponse response) {

        //表格名
        String filename = cmnyMessageSource.getMessage("excel.down.oldMan.template.shequ",null);

        ArrayList<String> names = new ArrayList<>();
        names.add(cmnyMessageSource.getMessage("excel.name", null));
        names.add(cmnyMessageSource.getMessage("excel.down.age", null));
        names.add(cmnyMessageSource.getMessage("excel.down.fender", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.IdCard", null));
        names.add(cmnyMessageSource.getMessage("excel.down.family.address.oldman", null));
        names.add(cmnyMessageSource.getMessage("excel.down.emergency.contact", null));
        names.add(cmnyMessageSource.getMessage("excel.down.emergency.contact.phone", null));
        names.add(cmnyMessageSource.getMessage("excel.down.relation.for.oldman", null));
        names.add(cmnyMessageSource.getMessage("excel.down.phone", null));
        names.add(cmnyMessageSource.getMessage("excel.down.family.address.ega", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.egaIdCard", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.nation", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.mStatus", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.oldEctLel", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.economicSource", null));
        names.add(cmnyMessageSource.getMessage("excel.down.oldman.oldMonIncome", null));

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            ServletOutputStream outputStream = response.getOutputStream();
            WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
            WritableSheet sheet = workbook.createSheet(filename, 0);

            //设置宽高度
            sheet.setRowView(0,600);
            sheet.setColumnView(0,15);
            sheet.setColumnView(1,7);
            sheet.setColumnView(2,10);
            sheet.setColumnView(3,30);
            sheet.setColumnView(4,25);
            sheet.setColumnView(5,15);
            sheet.setColumnView(6,20);
            sheet.setColumnView(7,12);
            sheet.setColumnView(8,20);
            sheet.setColumnView(9,25);
            sheet.setColumnView(10,25);
            sheet.setColumnView(11,12);
            sheet.setColumnView(12,15);
            sheet.setColumnView(13,15);
            sheet.setColumnView(14,15);
            sheet.setColumnView(15,15);

            //写入表头
            for (int i = 0; i < names.size(); i++) {
                sheet.addCell(new Label(i,0,names.get(i),ExcelUtil.wft2()));
            }

            //写入下拉选项框
            for (int i = 0; i < 300; i++) {

                sheet.setRowView(i,600);

                //性别
                Label gender = new Label(2, i+1,"  ");
                WritableCellFeatures wcf = new WritableCellFeatures();
                wcf.setDataValidationList(ExcelTemplateUtils.toGender());
                gender.setCellFeatures(wcf);
                sheet.addCell(gender);

                //紧急联系人与老人关系
                Label relationshipa = new Label(7, i+1,"  ");
                WritableCellFeatures wcf1 = new WritableCellFeatures();
                wcf1.setDataValidationList(ExcelTemplateUtils.toRelationshipa());
                relationshipa.setCellFeatures(wcf1);
                sheet.addCell(relationshipa);

                //民族
                Label nation = new Label(11, i+1,"  ");
                WritableCellFeatures wcf2 = new WritableCellFeatures();
                wcf2.setDataValidationList(ExcelTemplateUtils.toNation());
                nation.setCellFeatures(wcf2);
                sheet.addCell(nation);

                //老人婚姻状况
                Label mStatuss = new Label(12, i+1,"  ");
                WritableCellFeatures wcf3 = new WritableCellFeatures();
                wcf3.setDataValidationList(ExcelTemplateUtils.toMStatuss());
                mStatuss.setCellFeatures(wcf3);
                sheet.addCell(mStatuss);

                //老人文化程度
                Label oldEctLels = new Label(13, i+1,"  ");
                WritableCellFeatures wcf4 = new WritableCellFeatures();
                wcf4.setDataValidationList(ExcelTemplateUtils.toOldEctLels());
                oldEctLels.setCellFeatures(wcf4);
                sheet.addCell(oldEctLels);

                //老人经济来源
                Label economicSources = new Label(14, i+1,"  ");
                WritableCellFeatures wcf5 = new WritableCellFeatures();
                wcf5.setDataValidationList(ExcelTemplateUtils.toEconomicSources());
                economicSources.setCellFeatures(wcf5);
                sheet.addCell(economicSources);

                //老人月收入
                Label income = new Label(15, i+1,"  ");
                WritableCellFeatures wcf6 = new WritableCellFeatures();
                wcf6.setDataValidationList(ExcelTemplateUtils.toIncome());
                income.setCellFeatures(wcf6);
                sheet.addCell(income);
            }

            workbook.write();
            workbook.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
